package com.xinnet.mms.customtransfer.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.mms.common.AppGlobalContainer;
import com.xinnet.mms.config.dao.FlowTurnConfigDao;
import com.xinnet.mms.customerstate.dao.CustomerStateHistDao;
import com.xinnet.mms.customtransfer.service.CustomSystematizationService;
import com.xinnet.mms.empcustomrelation.dao.EmpCustomRelationDao;
import com.xinnet.mms.entity.CustomStateHist;
import com.xinnet.mms.entity.CustomerCare;
import com.xinnet.mms.entity.EmpCustomRelation;
import com.xinnet.mms.entity.PublicConfig;
import com.xinnet.mms.order.dao.OrderDao;
import com.xinnet.mms.product.dao.ProductDao;
import com.xinnet.mms.utils.CustomerManage;
@Service("customSystematizationService")
public class CustomSystematizationServiceImpl implements CustomSystematizationService{
	@Autowired
	public ProductDao productDao;
	@Autowired
	private EmpCustomRelationDao  empCustomRelationDao;
	@Autowired
	private CustomerStateHistDao customerStateHistDao;
	@Autowired
	private FlowTurnConfigDao flowTurnConfigDao;
	
	@Autowired
	private OrderDao orderDao;
	
	public void getBaseData(){

		AppGlobalContainer.productRuleMap = null;
		
		AppGlobalContainer.transferCustomMap = null;
		
		AppGlobalContainer.empCustomRelationMap = null;
		
		AppGlobalContainer.scoreHoldDateMap = null;
		
		//AppGlobalContainer.customerCareMap= null;
		
		AppGlobalContainer.delCustomerCareMap= null;
		
		this.getProductRuleMap();
		
		//this.getTransferCustomMapAndEmpCustomRelationMap();
		
		/*this.getCustomStateHistMap();*/
		
		/*this.getScoreHoldDateMap();	*/
		
		//this.getOrderList();
	}
	
	/**
	 * 获取流转规则
	 * @return Map<String,Product>
	 */
	private void getProductRuleMap(){
		AppGlobalContainer.productRuleMap = productDao.getEnableProduct();	
	}
	
	/**
	 * 获取客户信息
	 * @return
	 */
	@SuppressWarnings("unused")
	private void  getTransferCustomMapAndEmpCustomRelationMap(){

		Map<Integer,List<EmpCustomRelation>> customerMap = null;
		List<EmpCustomRelation> empCustomRelationList = null;//empCustomRelationDao.getTransferCustom();
		if(empCustomRelationList==null || empCustomRelationList.isEmpty()){
			return;
		}
		
		//新注册客户
		List<EmpCustomRelation> newRegisterCustomList = null;
		//意向客户
		List<EmpCustomRelation> intentionCustomList = null;
		//服务期客户
		List<EmpCustomRelation> ofServiceCustomList = null;
		//二次开发客户
		List<EmpCustomRelation> secondDevelopCustomList = null;
		//回收客户
		List<EmpCustomRelation> retrieveCustomList = null;
		//公海客户
		List<EmpCustomRelation> openSeaCustomList = null;
		
		for (int i = 0; i < empCustomRelationList.size(); i++) {
			EmpCustomRelation empCustomRelation = empCustomRelationList.get(i);
			Integer customerType = empCustomRelation.getCustomType();
			
			if(customerType == CustomerManage.NEW_CUSTOMER){
				if(newRegisterCustomList == null){
					newRegisterCustomList = new  ArrayList<EmpCustomRelation>();
				}
				newRegisterCustomList.add(empCustomRelation);
			}/*else if(customerType == CustomerManage.TWICE_NEW_CUSTOMER){
				if(newRegisterCustomList == null){
					newRegisterCustomList = new  ArrayList<EmpCustomRelation>();
				}
				newRegisterCustomList.add(empCustomRelation);
			}*/else if(customerType == CustomerManage.SERVICE_CUSTOMER){
				if(ofServiceCustomList == null){
					ofServiceCustomList = new  ArrayList<EmpCustomRelation>();
				}
				ofServiceCustomList.add(empCustomRelation);
				
			}/*else if(customerType == CustomerManage.INTENTION_CUSTOMER){
				if(intentionCustomList == null){
					intentionCustomList = new  ArrayList<EmpCustomRelation>();
				}
				intentionCustomList.add(empCustomRelation);
				
			}else if(customerType == CustomerManage.TWICE_CUSTOMER){
				if(secondDevelopCustomList == null){
					secondDevelopCustomList = new  ArrayList<EmpCustomRelation>();
				}
				secondDevelopCustomList.add(empCustomRelation);
				
			}*/else if(customerType == CustomerManage.RECYCLING_CUSTOMER){
				if(retrieveCustomList == null){
					retrieveCustomList = new  ArrayList<EmpCustomRelation>();
				}
				retrieveCustomList.add(empCustomRelation);
				
			}
		}
		customerMap = new HashMap<Integer, List<EmpCustomRelation>>();
		if(newRegisterCustomList!=null && newRegisterCustomList.size()>0){
			customerMap.put(CustomerManage.NEW_CUSTOMER, newRegisterCustomList);
		}
		/*if(secondDevelopCustomList!=null && secondDevelopCustomList.size()>0){
			customerMap.put(CustomerManage.TWICE_CUSTOMER, secondDevelopCustomList);
		}*/
		if(ofServiceCustomList!=null && ofServiceCustomList.size()>0){
			customerMap.put(CustomerManage.SERVICE_CUSTOMER, ofServiceCustomList);
		}
		if(openSeaCustomList!=null && openSeaCustomList.size()>0){
			customerMap.put(CustomerManage.OPEN_SEA_CUSTOMER, openSeaCustomList);
		}
		if(retrieveCustomList!=null && retrieveCustomList.size()>0){
			customerMap.put(CustomerManage.RECYCLING_CUSTOMER, retrieveCustomList);
		}
		/*if(intentionCustomList!=null && intentionCustomList.size()>0){
			customerMap.put(CustomerManage.INTENTION_CUSTOMER,intentionCustomList);
		}*/
		AppGlobalContainer.transferCustomMap=customerMap;
		
		
		Map<String,List<EmpCustomRelation>> empCustomRelationMap = new HashMap<String, List<EmpCustomRelation>>();
		for (Iterator<EmpCustomRelation> iterator = empCustomRelationList.iterator(); iterator.hasNext();) {
			EmpCustomRelation empCustomRelation = (EmpCustomRelation) iterator.next();
			String customId = empCustomRelation.getCustomId();
			boolean iscontainsKey = empCustomRelationMap.containsKey(customId);
			List<EmpCustomRelation> tempEmpCustomRelationList = null;
			if(iscontainsKey){
				tempEmpCustomRelationList = empCustomRelationMap.get(customId);
				tempEmpCustomRelationList.add(empCustomRelation);
			}else{
				tempEmpCustomRelationList = new ArrayList<EmpCustomRelation>();
				tempEmpCustomRelationList.add(empCustomRelation);
				empCustomRelationMap.put(customId, tempEmpCustomRelationList);
			}	
		}
		AppGlobalContainer.empCustomRelationMap = empCustomRelationMap;	
		empCustomRelationMap.clear();
	}
	
	/**
	 * 获取状态历史
	 * @return
	 */
	private  void getCustomStateHistMap(){
		Map<String,List<CustomStateHist>> customStateHistMap = new HashMap<String, List<CustomStateHist>>();
		List<CustomStateHist>  customStateHistList = customerStateHistDao.getCustomStateHist();
		for (Iterator<CustomStateHist> iterator = customStateHistList.iterator(); iterator.hasNext();) {
			CustomStateHist customStateHist = (CustomStateHist) iterator.next();
			String customId = customStateHist.getCustomId();
			boolean iscontainsKey = customStateHistMap.containsKey(customId);
			List<CustomStateHist> tempCustomStateHistList = null;
			if(iscontainsKey){
				tempCustomStateHistList = customStateHistMap.get(customId);
				tempCustomStateHistList.add(customStateHist);
			}else{
				tempCustomStateHistList = new ArrayList<CustomStateHist>();
				tempCustomStateHistList.add(customStateHist);
				customStateHistMap.put(customId, tempCustomStateHistList);
			}	
		}
		AppGlobalContainer.customStateHistMap = customStateHistMap;		
		
	}
	
	/**
	 * 获取业绩保留期
	 * @return
	 */
	private  void getScoreHoldDateMap(){
		PublicConfig  publicConfig = flowTurnConfigDao.getEnablePerfDayInCheck();
		AppGlobalContainer.scoreHoldDateMap= new  HashMap<String, PublicConfig>();
		AppGlobalContainer.scoreHoldDateMap.put("scoreHoldTime", publicConfig);
	}
	
	/**
	 * 获取订单列表
	 */
	private void getOrderList(){
		boolean isFirst = false;
		if(AppGlobalContainer.customerCareMap==null || AppGlobalContainer.customerCareMap.isEmpty()){
			AppGlobalContainer.customerCareMap = new HashMap<String, CustomerCare>();
			isFirst=true;
		}
		List<CustomerCare>  jianZhanOrderList = null;
		if(isFirst){
			jianZhanOrderList = orderDao.getJianZhanOrderAll();
		} else {
			jianZhanOrderList = orderDao.getJianZhanOrder();
		}
		for (Iterator<CustomerCare> iterator = jianZhanOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.customerCareMap.put(customId+"_"+productCode, customerCare);
			
		}
		
		List<CustomerCare> domainOrderList = null;
		if(isFirst){
			domainOrderList = orderDao.getDomainOrderAll();
		} else {
			domainOrderList = orderDao.getDomainOrder();
		}
		
		for (Iterator<CustomerCare> iterator = domainOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.customerCareMap.put(customId+"_"+productCode, customerCare);
			
		}
		
		List<CustomerCare> virtualHostOrderList = null;
		if(isFirst){
			virtualHostOrderList = orderDao.getVirtualHostOrderAll();
		} else {
			virtualHostOrderList = orderDao.getVirtualHostOrder();
		}
		for (Iterator<CustomerCare> iterator = virtualHostOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.customerCareMap.put(customId+"_"+productCode, customerCare);
			
		}
		
		List<CustomerCare> mailOrderList = null;
		if(isFirst){
			mailOrderList = orderDao.getMailOrderAll();
		} else {
			mailOrderList = orderDao.getMailOrder();
		}
		for (Iterator<CustomerCare> iterator = mailOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.customerCareMap.put(customId+"_"+productCode, customerCare);
			
		}
		List<CustomerCare> zJZYOrderList = null;
		if(isFirst){
			zJZYOrderList = orderDao.getZJZYOrderAll();
		} else {
			zJZYOrderList = orderDao.getZJZYOrder();
		}
		for (Iterator<CustomerCare> iterator = zJZYOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.customerCareMap.put(customId+"_"+productCode, customerCare);
		}
		List<CustomerCare>  nYZJOrXNYYOrderList = null;
		if(isFirst){
			nYZJOrXNYYOrderList = orderDao.getNYZJOrXNYYOrderAll();
		} else {
			nYZJOrXNYYOrderList = orderDao.getNYZJOrXNYYOrder();
		}
		for (Iterator<CustomerCare> iterator = nYZJOrXNYYOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.customerCareMap.put(customId+"_"+productCode, customerCare);
		}
		AppGlobalContainer.delCustomerCareMap = new HashMap<String, CustomerCare>();
		List<CustomerCare>  deledDomainOrderList = orderDao.getDeledDomainOrder();
		for (Iterator<CustomerCare> iterator = deledDomainOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.delCustomerCareMap.put(customId+"_"+productCode,customerCare);
		}
		List<CustomerCare>  deledVirtualHostOrderList = orderDao.getDeledVirtualHostOrder();
		for (Iterator<CustomerCare> iterator = deledVirtualHostOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.delCustomerCareMap.put(customId+"_"+productCode, customerCare);
		}
		 List<CustomerCare> deledMailOrderList =  orderDao.getDeledMailOrder();
		for (Iterator<CustomerCare> iterator = deledMailOrderList.iterator(); iterator.hasNext();){
			CustomerCare customerCare = (CustomerCare) iterator.next();
			String customId = customerCare.getCusId();
			String productCode = customerCare.getProductId();
			AppGlobalContainer.delCustomerCareMap.put(customId+"_"+productCode, customerCare);
		}	
	}

	
	
	/**
	 * 客户分类
	 */
	public Map<Integer,List<EmpCustomRelation>>  customSystematization(){
		
		Map<Integer,List<EmpCustomRelation>> customerMap = this.loadCustomerFromDB();
		
		return customerMap;
	}
	/**
	 * 从缓存加载数据
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "unused" })
/*	private synchronized Map<Integer,List<Customer>> loadCustomerFromCache() throws Exception{
		Map<Integer,List<Customer>> customerMap = (Map<Integer,List<Customer>>)memcachedClient.get(MemcachedKeys.getCustomerKey());
		return customerMap;
	}*/
	/**
	 * 从数据库加载数据
	 * @return
	 */
	private synchronized Map<Integer,List<EmpCustomRelation>> loadCustomerFromDB(){
		Map<Integer,List<EmpCustomRelation>> customerMap = null;
		List<EmpCustomRelation> empCustomRelationList = null;//empCustomRelationDao.getTransferCustom();
		if(empCustomRelationList==null || empCustomRelationList.isEmpty()){
			return null;
		}
		
		//新注册客户
		List<EmpCustomRelation> newRegisterCustomList = null;
		//意向客户
		List<EmpCustomRelation> intentionCustomList = null;
		//服务期客户
		List<EmpCustomRelation> ofServiceCustomList = null;
		//二次开发客户
		List<EmpCustomRelation> secondDevelopCustomList = null;
		//回收客户
		List<EmpCustomRelation> retrieveCustomList = null;
		//公海客户
		List<EmpCustomRelation> openSeaCustomList = null;
		
		for (int i = 0; i < empCustomRelationList.size(); i++) {
			EmpCustomRelation empCustomRelation = empCustomRelationList.get(i);
			Integer customerType = empCustomRelation.getCustomType();
			
			if(customerType == CustomerManage.NEW_CUSTOMER){
				if(newRegisterCustomList == null){
					newRegisterCustomList = new  ArrayList<EmpCustomRelation>();
				}
				newRegisterCustomList.add(empCustomRelation);
			}else if(customerType == CustomerManage.TWICE_NEW_CUSTOMER){
				if(newRegisterCustomList == null){
					newRegisterCustomList = new  ArrayList<EmpCustomRelation>();
				}
				newRegisterCustomList.add(empCustomRelation);
			}else if(customerType == CustomerManage.SERVICE_CUSTOMER){
				if(ofServiceCustomList == null){
					ofServiceCustomList = new  ArrayList<EmpCustomRelation>();
				}
				ofServiceCustomList.add(empCustomRelation);
				
			}else if(customerType == CustomerManage.INTENTION_CUSTOMER){
				if(intentionCustomList == null){
					intentionCustomList = new  ArrayList<EmpCustomRelation>();
				}
				intentionCustomList.add(empCustomRelation);
				
			}else if(customerType == CustomerManage.TWICE_CUSTOMER){
				if(secondDevelopCustomList == null){
					secondDevelopCustomList = new  ArrayList<EmpCustomRelation>();
				}
				secondDevelopCustomList.add(empCustomRelation);
				
			}else if(customerType == CustomerManage.RECYCLING_CUSTOMER){
				if(retrieveCustomList == null){
					retrieveCustomList = new  ArrayList<EmpCustomRelation>();
				}
				retrieveCustomList.add(empCustomRelation);
				
			}
		}
		customerMap = new HashMap<Integer, List<EmpCustomRelation>>();
		if(newRegisterCustomList!=null && newRegisterCustomList.size()>0){
			customerMap.put(CustomerManage.NEW_CUSTOMER, newRegisterCustomList);
		}
		if(secondDevelopCustomList!=null && secondDevelopCustomList.size()>0){
			customerMap.put(CustomerManage.TWICE_CUSTOMER, secondDevelopCustomList);
		}
		if(ofServiceCustomList!=null && ofServiceCustomList.size()>0){
			customerMap.put(CustomerManage.SERVICE_CUSTOMER, ofServiceCustomList);
		}
		if(openSeaCustomList!=null && openSeaCustomList.size()>0){
			customerMap.put(CustomerManage.OPEN_SEA_CUSTOMER, openSeaCustomList);
		}
		if(retrieveCustomList!=null && retrieveCustomList.size()>0){
			customerMap.put(CustomerManage.RECYCLING_CUSTOMER, retrieveCustomList);
		}
		if(intentionCustomList!=null && intentionCustomList.size()>0){
			customerMap.put(CustomerManage.INTENTION_CUSTOMER,intentionCustomList);
		}
		return customerMap;
	}
}
